<?php
class Operate_power_log_model extends CI_Model
{
    private $table = 'operate_power_log';
    
    public function total($params=array())
    {
        if (!empty($params['search_name'])) {
            $this->db->where("((`content` LIKE '%{$params['search_name']}%') OR (`name` LIKE '%{$params['search_name']}%'))");
        }
        if (!empty($params['record'])) {
            $this->db->like('record', $params['record']);
        }
        if (!empty($params['content'])) {
            $this->db->like('content', $params['content']);
        }
        if (!empty($params['start_date'])) {
            $this->db->where(array('create_time >=' => $params['start_date'].' 00:00:00'));
        }
        if (!empty($params['end_date'])) {
            $this->db->where(array('create_time <=' => $params['end_date'].' 23:59:59'));
        }
        return $this->db->count_all_results($this->table);
    }
    
    public function page_list($page_num, $num, $params=array())
    {
        if (!empty($params['search_name'])) {
            $this->db->where("((`content` LIKE '%{$params['search_name']}%') OR (`name` LIKE '%{$params['search_name']}%'))");
        }
        if (!empty($params['record'])) {
            $this->db->like('record', $params['record']);
        }
        if (!empty($params['content'])) {
            $this->db->like('content', $params['content']);
        }
        if (!empty($params['start_date'])) {
            $this->db->where(array('create_time >=' => $params['start_date'].' 00:00:00'));
        }
        if (!empty($params['end_date'])) {
            $this->db->where(array('create_time <=' => $params['end_date'].' 23:59:59'));
        }
        $this->db->order_by('id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get($this->table);
    }
    
    public function insertOperatePowerLog($params=array())
    {
        $data = array(
            'uid'     => $params['uid'],
            'name'    => !empty($params['name']) ? $params['name'] : '未知',
            'record'  => $params['record'],
            'content' => $params['content'],
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
}